package com.ezcms.utils;
import org.im4java.core.ConvertCmd;
import org.im4java.core.IMOps;
import org.im4java.core.IdentifyCmd;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.*;
import javax.imageio.ImageIO;
import org.im4java.core.IMOperation;
import org.im4java.process.ArrayListOutputConsumer;

/**
 * ImageMagick和im4java处理图片 
 * 
 * @author sunlightcs  http://hi.juziku.com/sunlightcs/ 
 */

public class ImageTools {

	/**
	 * ImageMagick的路径 
	 */

	 public static String imageMagickPath = null;
	 private static double quality=50;

	static {

		/**
		 *  获取ImageMagick的路径 
		 */

		// Properties prop = new PropertiesFile().getPropertiesFile();

		// linux下不要设置此值，不然会报错

		//imageMagickPath = SpringSession.getServerConfigManager().getConfig("ImageMagickPath");
		//imageMagickPath="E:\\Program Files\\ImageMagick-6.3.9-Q8";

	}

	/**
	 *  根据坐标裁剪图片 
	 * 
	 * 
	 * 
	 * @param srcPath
	 *            要裁剪图片的路径 
	 * @param newPath
	 *            裁剪图片后的路径 
	 * @param width
	 *             宽
	 * @param height
	 *            高 
	 * @param x
	 *            左上角x轴 
	 * @param y
	 *            左上角y轴 
	 */
	public static void cutImage(String srcPath, String newPath, int width, int height,
			int x, int y) throws Exception {

		//int width = x1 - x;

		//int height = y1 - y;

		IMOperation op = creatIMOperation();

		op.addImage(srcPath);

		/**
		 *  width：裁剪的宽度  height：裁剪的高度  x：裁剪的横坐标 y：裁剪的挫坐标 
		 */
 
		op.crop(width, height, x, y);

		op.addImage(newPath);
		
 

		ConvertCmd convert = new ConvertCmd();

		// linux下不要设置此值，不然会报错

		convert.setSearchPath(imageMagickPath);

		convert.run(op);

	}

	/**
	 *  根据尺寸缩放图片 
	 * 
	 * @param width
	 *            缩放后的图片宽度 
	 * @param height
	 *            缩放后的图片高度 
	 * @param srcPath
	 *            源图片路径 
	 * @param newPath
	 *            缩放后图片的路径 
	 */

	public static void cutImage(int width, int height, String srcPath, String newPath) throws Exception {
		IMOperation op = creatIMOperation();
		op.addImage(srcPath);
		op.resize(width, height);
		op.addImage(newPath);
		ConvertCmd convert = new ConvertCmd();
		// linux下不要设置此值，不然会报错
		convert.setSearchPath(imageMagickPath);
		System.out.println("~~~~~~~~~~~~切割图片~~~~~~~~~~~~~~");
		System.out.println("width=" + width);
		System.out.println("srcPath=" + srcPath);
		System.out.println("newPath=" + newPath);
		System.out.println("imageMagickPath=" + imageMagickPath);
		System.out.println("~~~~~~~~~~~~切割图片~~~~~~~~~~~~~~");
		convert.run(op);
	}

	/**
	 * 根据宽度缩放图片
	 * 
	 * @param width
	 *            缩放后的图片宽度
	 * @param srcPath
	 *            源图片路径
	 * @param newPath
	 *            缩放后图片的路径
	 */

	public static void cutImage(int width, String srcPath, String newPath) throws Exception {
		IMOperation op = creatIMOperation();
		op.addImage(srcPath);
		op.resize(width, null);
		op.addImage(newPath);
		ConvertCmd convert = new ConvertCmd();
		// linux下不要设置此值，不然会报错
		convert.setSearchPath(imageMagickPath);
		System.out.println("~~~~~~~~~~~~切割图片~~~~~~~~~~~~~~");
		System.out.println("width=" + width);
		System.out.println("srcPath=" + srcPath);
		System.out.println("newPath=" + newPath);
		System.out.println("imageMagickPath=" + imageMagickPath);
		System.out.println("~~~~~~~~~~~~切割图片~~~~~~~~~~~~~~");
		convert.run(op);

	}

	/**
	 * 给图片加水印 
	 * 
	 * @param srcPath
	 *            源图片路径 
	 */

	public static void addImgText(String srcPath) throws Exception {

		IMOperation op = creatIMOperation();

		op.font("宋体").gravity("southeast").pointsize(18).fill("#BCBFC8")
				.draw("text 5,5 juziku.com");

		op.addImage();

		op.addImage();

		ConvertCmd convert = new ConvertCmd();

		// linux下不要设置此值，不然会报错
	    convert.setSearchPath(imageMagickPath);
		convert.run(op, srcPath, srcPath);

	}
	/**
	 * 图片旋转
	 * @param srcImagePath
	 * @param destImagePath
	 * @param angle
	 * @throws Exception
	 * @author ZHF
	 * @version 2011-6-21上午10:53:02
	 */
	public static void rotate(String srcImagePath, String destImagePath, double angle) throws Exception {  
	       
	           IMOperation op = creatIMOperation(); 
	           op.rotate(angle);  
	           op.addImage(srcImagePath);  
	           op.addImage(destImagePath);  
	           ConvertCmd convert = new ConvertCmd();  
	           convert.setSearchPath(imageMagickPath);
	           convert.run(op);  
	       
	   }  
	/**
	 * 图片信息显示
	 * @param args
	 * @throws Exception
	 * @author ZHF
	 * @version 2011-6-21上午11:08:29
	 */
	public static String showImageInfo(String imagePath) throws Exception {  
  
            IMOperation op = creatIMOperation();  
            op.format("%w#%h#%d#%f#%b#%[EXIF:DateTimeOriginal]");  
            op.addImage(1);  
            IdentifyCmd identifyCmd = new IdentifyCmd();  
            ArrayListOutputConsumer output = new ArrayListOutputConsumer();  
            identifyCmd.setOutputConsumer(output);
            identifyCmd.setSearchPath(imageMagickPath);
            identifyCmd.run(op,imagePath);  
         
           List <String> cmdOutput = output.getOutput();  
            assert cmdOutput.size() == 1;  
            String line = cmdOutput.get(0);  
            return line; 
	}
	/**
	 * 取得图片高和宽
	 * @param src
	 * @return
	 * @author ZHF
	 * @version 2011-6-21上午11:29:04
	 */
	public static Map<String,Integer> getWidthAndHeight(String src)
	{
		File file=new File(src);
		Map<String,Integer> map=null;
		try {
			BufferedImage bi =ImageIO.read(file);
			map=new HashMap();
			map.put("width", bi.getWidth());
			map.put("height", bi.getHeight());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return map;
	}
	/**
	 * 设置宽度不能超过的值进行压缩
	 * @param srcPath
	 * @param newPath
	 * @throws Exception
	 * @author ZHF
	 * @version 2011-6-21上午11:32:05
	 */
	public static void resize(String srcPath, String newPath, int maxWidth) {

		try {
			IMOperation op = creatIMOperation();
			op.addImage(srcPath);
			int width = getWidthAndHeight(srcPath).get("width");
			int height = getWidthAndHeight(srcPath).get("height");
			if (width > maxWidth) {
				System.out.println("width > maxWidth");
				cutImage(maxWidth, srcPath, newPath);

			} else {
				System.out.println("width <=maxWidth");
				cutImage(width, height, srcPath, newPath);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	
	private static IMOperation creatIMOperation() {
		IMOperation op = new IMOperation();
		op.quality(quality);
		return op;
	}
	
	
	

	public static void main(String[] args) throws Exception {
		ImageTools.imageMagickPath = "D:\\Program Files\\ImageMagick";
		String path="C:\\Users\\Administrator\\Desktop\\照片";
		for(int i=1 ;i<=2;i++){
			ImageTools.quality= 80;
			ImageTools.resize(path + "\\test"+i+".jpg" , path+ "\\test" +i+".bak.jpg", 1000);
		} 
	}

}
